package com.credithc.signature.job.job;

import com.credithc.signature.common.utils.DateUtil;
import com.credithc.signature.job.service.StatisticsReqService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @desc: <br/>
 * @author: Biyun Fu<br/>
 * @time：2021年06⽉30⽇<br/>
 * @version：v1.0.0<br>
 */
@Slf4j
@Component
public class StatisticsReqJob {

    @Autowired
    private StatisticsReqService statisticsReqService;

    /**
     * 1、定时任务统计失败合同
     */
    @XxlJob("statisticsRequestFailHandler")
    public void statisticsRequestFailHandler() throws Exception {
        log.info("开始执行【签章失败统计】定时任务 >>>>>> 现在时间：{}", new Date());
        List<String> list = getTime();
        statisticsReqService.statisticsRequestFail(list.get(0), list.get(1));
    }

    /**
     * 2、定时任务统计超时合同
     */
    @XxlJob("statisticsRequestTimeHandler")
    public void statisticsRequestTimeHandler() throws Exception {
        log.info("开始执行【签章超时统计】定时任务 >>>>>> 现在时间：{}", new Date());
        List<String> list = getTime();
        statisticsReqService.statisticsRequestTime(list.get(0), list.get(1));
    }

    /**
     * 3、定时任务统计当天各业务系统
     */
    @XxlJob("statisticsRequestDayFailHandler")
    public void statisticsRequestDayFailHandler() throws Exception {
        Date yesterday = DateUtil.getYesterdayStanderDate();
        Date startTime = DateUtil.getFirstTimeOfDay(yesterday);
        Date endTime = DateUtil.getLastTimeOfDay(yesterday);
        log.info("开始执行【签章昨天失败统计】定时任务 >>>>>> 现在时间：{}", DateUtil.getNowDate());
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        statisticsReqService.statisticsRequestDayFail(df.format(startTime), df.format(endTime));
    }

    /**
     * 获取统计时间区间
     */
    public List<String> getTime() throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTimeStr = df.format(new Date());
        Date now = df.parse(nowTimeStr);
        Date startTime = DateUtil.getBeforeMinuteByStartTime(5, now);
        Date endTime = DateUtil.getBeforeMinuteByStartTime(0, now);
        List<String> list = new ArrayList<>();
        list.add(df.format(startTime));
        list.add(df.format(endTime));
        return list;
    }
}
